package pl.droidsonroids.gif;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.widget.MediaController.MediaPlayerControl;

import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Locale;

public class GifDrawable extends Drawable implements Animatable, MediaPlayerControl {
    public int a;
    protected final int[] b;
    public final Runnable c;
    public h d;
    private static final Handler e;
    private volatile int f;
    private volatile boolean g;
    private final int[] h;
    private float i;
    private float j;
    private boolean k;
    private final Rect l;
    private Paint m;
    private final Runnable n;
    private final Runnable o;
    private final Runnable p;
    private final Runnable q;
    private final Runnable r;

    static {
        try {
            System.loadLibrary("gif");
        } catch (UnsatisfiedLinkError v0_1) {
        }

        e = new Handler(Looper.getMainLooper());
    }

    public GifDrawable(String filePath) throws IOException {
        this.g = true;
        this.h = new int[5];
        this.i = 1f;
        this.j = 1f;
        this.l = new Rect();
        this.a = -1;
        this.m = new Paint(6);
        this.c = new Runnable() {
            @Override
            public final void run() {
                GifDrawable.reset(GifDrawable.this.f);
            }
        };
        this.n = new Runnable() {
            @Override
            public final void run() {
                GifDrawable.restoreRemainder(GifDrawable.this.f);
                invalidateSelf();
            }
        };
        this.o = new Runnable() {
            public final void run() {
                GifDrawable.saveRemainder(GifDrawable.this.f);
            }
        };
        this.p = new Runnable() {
            public final void run() {
                invalidateSelf();
            }
        };
        this.q = new Runnable() {
            @Override
            public final void run() {
                if (GifDrawable.b(GifDrawable.this) != null) {
                    GifDrawable.b(GifDrawable.this).b();
                }
            }
        };
        this.r = new Runnable() {
            @Override
            public final void run() {
                if (GifDrawable.b(GifDrawable.this) != null) {
                    GifDrawable.b(GifDrawable.this).a();
                }
            }
        };
        if (filePath == null) {
            throw new NullPointerException("Source is null");
        }

        new File(filePath).length();
        this.f = GifDrawable.openFile(this.h, filePath);
        this.b = new int[this.h[0] * this.h[1]];
    }

    public static void a(Runnable arg2) {
        if (Looper.myLooper() == GifDrawable.e.getLooper()) {
            arg2.run();
        } else {
            GifDrawable.e.post(arg2);
        }
    }

    static int a(int arg1, int arg2, int[] arg3) {
        return GifDrawable.seekToTime(arg1, arg2, arg3);
    }

    static int a(GifDrawable arg1) {
        return arg1.f;
    }

    static void a(int arg0) {
        GifDrawable.reset(arg0);
    }

    static int b(int arg1) {
        return GifDrawable.restoreRemainder(arg1);
    }

    static h b(GifDrawable arg1) {
        return arg1.d;
    }

    static int c(int arg1) {
        return GifDrawable.saveRemainder(arg1);
    }

    public boolean canPause() {
        return true;
    }

    public boolean canSeekBackward() {
        return false;
    }

    public boolean canSeekForward() {
        boolean v0 = true;
        if (this.h[2] <= 1) {
            v0 = false;
        }

        return v0;
    }

    public void draw(Canvas arg12) {
        int v0 = 0;
        try {
            if (this.k) {
                this.l.set(this.getBounds());
                this.i = (((float) this.l.width())) / (((float) this.h[0]));
                this.j = (((float) this.l.height())) / (((float) this.h[1]));
                this.k = false;
            }

            if (this.m.getShader() != null) {
                arg12.drawRect(this.l, this.m);
                return;
            }

            if (GifDrawable.getCurrentIndex(this.f) == this.h[2] - 1) {
                v0 = 1;
            }

            if (v0 != 0) {
                if (this.a == 0) {
                    this.g = false;
                    this.h[4] = -1;
                    if (this.d != null) {
                        GifDrawable.a(this.q);
                    }
                } else if (this.d != null) {
                    GifDrawable.a(this.r);
                }
            }

            label_51:
            if (this.g) {
                GifDrawable.renderFrame(this.b, this.f, this.h);
            } else {
                this.h[4] = -1;
            }

            arg12.scale(this.i, this.j);
            arg12.drawBitmap(this.b, 0, this.h[0], 0f, 0f, this.h[0], this.h[1], true, this.m);
            if (this.h[4] >= 0 && this.h[2] > 1) {
                GifDrawable.e.postDelayed(this.p, ((long) this.h[4]));
            }

            return;
        } catch (Throwable v0_1) {
            return;
        }
    }

    protected void finalize() throws Throwable {
        try {
            this.g = false;
            int v0_1 = this.f;
            this.f = 0;
            GifDrawable.free(v0_1);
        } catch (Throwable v0) {
            super.finalize();
            throw v0;
        }

        super.finalize();
    }

    private static native void free(int arg0);

    private static native long getAllocationByteCount(int arg0);

    public int getAlpha() {
        return this.m.getAlpha();
    }

    public int getAudioSessionId() {
        return 0;
    }

    public int getBufferPercentage() {
        return 100;
    }

    private static native String getComment(int arg0);

    private static native int getCurrentIndex(int arg0);

    private static native int getCurrentPosition(int arg0);

    public int getCurrentPosition() {
        return GifDrawable.getCurrentPosition(this.f);
    }

    private static native int getDuration(int arg0);

    public int getDuration() {
        return GifDrawable.getDuration(this.f);
    }

    public int getIntrinsicHeight() {
        return this.h[1];
    }

    public int getIntrinsicWidth() {
        return this.h[0];
    }

    private static native int getLoopCount(int arg0);

    public int getMinimumHeight() {
        return this.h[1];
    }

    public int getMinimumWidth() {
        return this.h[0];
    }

    public int getOpacity() {
        return -2;
    }

    public boolean isPlaying() {
        return this.g;
    }

    public boolean isRunning() {
        return this.g;
    }

    protected void onBoundsChange(Rect arg2) {
        super.onBoundsChange(arg2);
        this.k = true;
    }

    private static native int openByteArray(int[] arg0, byte[] arg1);

    private static native int openDirectByteBuffer(int[] arg0, ByteBuffer arg1);

    private static native int openFd(int[] arg0, FileDescriptor arg1, long arg2);

    private static native int openFile(int[] arg0, String arg1);

    private static native int openStream(int[] arg0, InputStream arg1);

    public void pause() {
        this.stop();
    }

    private static native void renderFrame(int[] arg0, int arg1, int[] arg2);

    private static native void reset(int arg0);

    private static native int restoreRemainder(int arg0);

    private static native int saveRemainder(int arg0);

    public void seekTo(final int position) {
        if (position < 0) {
            throw new IllegalArgumentException("Position is not positive");
        }

        GifDrawable.a(new Runnable() {
            @Override
            public final void run() {
                GifDrawable.a(GifDrawable.a(GifDrawable.this), position, GifDrawable.this.b);
                GifDrawable.this.invalidateSelf();
            }
        });
    }

    private static native int seekToFrame(int arg0, int arg1, int[] arg2);

    private static native int seekToTime(int arg0, int arg1, int[] arg2);

    public void setAlpha(int arg2) {
        this.m.setAlpha(arg2);
    }

    public void setColorFilter(ColorFilter arg2) {
        this.m.setColorFilter(arg2);
    }

    public void setDither(boolean arg2) {
        this.m.setDither(arg2);
        this.invalidateSelf();
    }

    public void setFilterBitmap(boolean arg2) {
        this.m.setFilterBitmap(arg2);
        this.invalidateSelf();
    }

    private static native void setSpeedFactor(int arg0, float arg1);

    public void start() {
        this.g = true;
        GifDrawable.a(this.n);
    }

    public void stop() {
        this.g = false;
        GifDrawable.a(this.o);
    }

    public String toString() {
        return String.format(Locale.US, "Size: %dx%d, %d frames, error: %d", Integer.valueOf(this.h[0]), Integer.valueOf(this.h[1]), Integer.valueOf(this.h[2]), Integer.valueOf(this.h[3]));
    }
}

